/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.ComponentModel;

namespace DNNContentBuilder
{
	/// <summary>
	/// Enumerate possible statuses for Task rows.
	/// </summary>
	[TypeConverter(typeof(TaskStatusConverter))]
	public enum TaskStatus
	{
		NotAssigned	= '\0',
		Assigned	= 'A',
		Working		= 'W',
		Rejected	= 'R',
		Finished	= 'F'
	};

	/// <summary>
	/// Type converter for the <see cref="TaskStatus"/> enumeration.
	/// </summary>
	public class TaskStatusConverter : TypeConverter
	{
		/// <summary>
		/// Overrides <see cref="TypeConverter.CanConvertFrom"/>.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="sourceType"></param>
		/// <returns></returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			if (sourceType == typeof(string))
			{
				return true;
			}

			return base.CanConvertFrom (context, sourceType);
		}

		/// <summary>
		/// Overrides <see cref="TypeConverter.CanConvertTo"/>.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="destinationType"></param>
		/// <returns></returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				return true;
			}

			return base.CanConvertTo (context, destinationType);
		}

		/// <summary>
		/// Overrides <see cref="TypeConverter.ConvertFrom"/>.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (value is string)
			{
				// Perform some health checks
				string unboxedString = value as string;

				if (unboxedString == "\0" || unboxedString.Length == 0)
				{
					return TaskStatus.NotAssigned;
				}

				return (TaskStatus)(int)((string)value)[0];
			}

			return base.ConvertFrom(context, culture, value);
		}

		/// <summary>
		/// Will convert <see cref="TaskStatus"/> values to their corresponding 
		/// <see cref="System.String"/> representations.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <param name="destinationType"></param>
		/// <returns></returns>
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				return Char.ToString((char)(TaskStatus)value);
			}

			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}
